#!/bin/bash

cleanerror(){

  cd ../../
  rm -rf bin/$(uname -s)$"-"$(clang -dumpmachine)
  if [ ! "$(ls -A $bin)" ]; then
  
    rm -rf bin/
	
  fi
  
  exit

}

mkdir -p bin

if [[ "$OSTYPE" == "linux-gnu"* ]]; then

  mkdir -p bin/$(uname -s)$"-"$(clang -dumpmachine)
  cd bin/$(uname -s)$"-"$(clang -dumpmachine) || exit

elif [[ "$OSTYPE" == "darwin"* ]]; then

  flags="-lglut -lGLU -lGL"
  mkdir -p bin/$(uname -s)$"-"$(clang -dumpmachine)
  cd bin/$(uname -s)$"-"$(clang -dumpmachine) || exit

elif [[ "$OSTYPE" == "cygwin" ]]; then

  flags="-lopengl32"
  mkdir -p bin/$(uname -s)$"-"$(clang -dumpmachine)
  cd bin/$(uname -s)$"-"$(clang -dumpmachine) || exit

elif [[ "$OSTYPE" == "msys" ]]; then

  flags="-lopengl32"
  mkdir -p bin/$(uname -s)$"-"$(clang -dumpmachine)
  cd bin/$(uname -s)$"-"$(clang -dumpmachine) || exit

elif [[ "$OSTYPE" == "win32" ]]; then

  flags="-lopengl32"
  mkdir -p bin/$(uname -s)$"-"$(clang -dumpmachine)
  cd bin/$(uname -s)$"-"$(clang -dumpmachine) || exit

elif [[ "$OSTYPE" == "freebsd"* ]]; then

  mkdir -p bin/$(uname -s)$"-"$(clang -dumpmachine)
  cd bin/$(uname -s)$"-"$(clang -dumpmachine) || exit

else

  mkdir -p bin/$(uname -s)$"-"$(clang -dumpmachine)
  cd bin/$(uname -s)$"-"$(clang -dumpmachine) || exit
  
fi

rm -rf -f *
clang++ -o lighty ../../src/main.cpp -std=c++17 -Wall -Wextra -O2 $(pkg-config --libs --cflags glew sdl2 SDL2_mixer SDL2_ttf jsoncpp) $flags || cleanerror
cp -r ../../example/* . 2>/dev/null
